package com.cheetah.start.module.system.service.impl;

import com.cheetah.start.common.property.UpLoadProperty;
import com.cheetah.start.common.utils.FileUtil;
import com.cheetah.start.module.system.domain.SysFile;
import com.cheetah.start.module.system.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 本地上传服务
 *
 * @author xjh
 * @date 2021/12/23
 */
@Service
public class UploadLocalServiceImpl implements IUploadService {
    @Autowired
    private UpLoadProperty upLoadProperty;

    /**
     * 上传
     *
     * @param file       文件
     * @param moduleName
     * @return {@link String}
     */
    @Override
    public SysFile upload(MultipartFile file, String moduleName) throws IOException {
        SysFile sysFile = SysFile.getFile(file, upLoadProperty, moduleName);
        File fileHave = FileUtil.getFileAndCheck(sysFile.getFileRealPath());
        // 文件上传
        file.transferTo(fileHave);
        return sysFile;
    }
}
